//
//  NSString+BZCreated.m
//  BZFoundation
//
//  Created by 郭艺伟 on 2017/10/1.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "NSString+BZCreated.h"
inline NSString *bz_NSStringFromBool(BOOL flag) {
    return flag ? @"YES" : @"NO";
}

@implementation NSString (BZCreated)
- (instancetype)bz_trimString {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

+ (instancetype)bz_stringByCallstackSymbols:(NSArray *)callstack {
    if (!callstack) {
        return @"";
    }
    
    if (![callstack isKindOfClass:[NSArray class]]) {
        return @"";
    }
    
    if ([callstack count] < 6) {
        return @"";
    }
    
    return [NSString stringWithFormat:@"[%@ %@]:%@", callstack[3], callstack[4], callstack[5]];
}

+ (instancetype)bz_stringWithNullableCString:(const char *)pStr {
    if (pStr == NULL) {
        return @"";
    }
    
    return [NSString stringWithUTF8String:pStr];
}

+ (instancetype)bz_blowFishString {
    // BlowFish的长度是16-64位
    return [self bz_randomCodeWithLenghtInterval:NSMakeRange(16, 48)];
}

+ (instancetype)bz_randomCodeWithLenghtInterval:(NSRange)lenghtInterval {
    if (lenghtInterval.location == 0) {
        return nil;
    }
    
    // 随机数长度区间在[lenghtInterval.location NSMaxRange(lenghtInterval)]
    static const NSString *kRandomAlphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *randomString = [NSMutableString stringWithCapacity:lenghtInterval.location];
    for (int i = 0; i < lenghtInterval.location; i++) {
        [randomString appendFormat: @"%C", [kRandomAlphabet characterAtIndex:arc4random_uniform((u_int32_t)[kRandomAlphabet length])]];
    }
    return [randomString copy];
}


@end
